package com.meeting.controller.admin;

import com.meeting.dto.RoomDTO;
import com.meeting.dto.RoomQueryDTO;
import com.meeting.dto.RoomStatusDTO;
import com.meeting.entity.Category;
import com.meeting.entity.EquipmentType;
import com.meeting.entity.Rooms;
import com.meeting.service.CategoryService;
import com.meeting.service.EquipmentTypeService;
import com.meeting.service.RoomsService;
import com.meeting.vo.PageResult;
import com.meeting.vo.Result;
import com.meeting.vo.RoomsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/room-manage")
@Tag(name = "管理员会议室管理接口")
public class AdminRoomManageController {
    
    @Autowired
    private RoomsService roomsService;
    
    @Autowired
    private EquipmentTypeService equipmentTypeService;
    
    @Autowired
    private CategoryService categoryService;
    
    @GetMapping
    @Operation(summary = "分页查询会议室")
    public Result listRooms(RoomQueryDTO roomQueryDTO) {
        log.info("管理员分页查询会议室信息");
        PageResult<RoomsVO> rooms = roomsService.listRooms(roomQueryDTO);
        return Result.success(rooms);
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "获取会议室详情")
    public Result getRoomDetail(@PathVariable Integer id) {
        log.info("管理员获取会议室详情，ID：{}", id);
        RoomsVO room = roomsService.getRoomDetail(id);
        if (room == null) {
            return Result.error("会议室不存在");
        }
        return Result.success(room);
    }
    
    @GetMapping("/check-room-availability/{id}")
    @Operation(summary = "检查会议室是否可修改或删除")
    public Result checkRoomAvailability(@PathVariable Integer id) {
        log.info("检查会议室是否可修改或删除，ID：{}", id);
        boolean canModify = roomsService.checkRoomModifiable(id);
        return Result.success(canModify);
    }
    
    @PostMapping
    @Operation(summary = "新增会议室")
    public Result addRoom(@RequestBody RoomDTO roomDTO) {
        log.info("管理员新增会议室：{}", roomDTO);
        boolean result = roomsService.addRoom(roomDTO);
        return result ? Result.success() : Result.error("新增会议室失败");
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "修改会议室")
    public Result updateRoom(@PathVariable Integer id, @RequestBody RoomDTO roomDTO) {
        log.info("管理员修改会议室，ID：{}，数据：{}", id, roomDTO);
        roomDTO.setId(id);
        boolean result = roomsService.updateRoom(roomDTO);
        return result ? Result.success() : Result.error("修改会议室失败");
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除会议室")
    public Result deleteRoom(@PathVariable Integer id) {
        log.info("管理员删除会议室，ID：{}", id);
        boolean result = roomsService.deleteRoom(id);
        return result ? Result.success() : Result.error("删除会议室失败");
    }
    
    @PutMapping("/status/{id}")
    @Operation(summary = "修改会议室状态")
    public Result updateRoomStatus(@PathVariable Integer id, @RequestParam Integer status) {
        log.info("管理员修改会议室状态，ID：{}，状态：{}", id, status);
        boolean result = roomsService.updateRoomStatus(id, status);
        return result ? Result.success() : Result.error("修改会议室状态失败");
    }
    
    @PutMapping("/toggle-status/{id}")
    @Operation(summary = "切换会议室状态")
    public Result toggleRoomStatus(@PathVariable Integer id, @RequestBody RoomStatusDTO roomStatusDTO) {
        log.info("管理员切换会议室状态，ID：{}，状态：{}", id, roomStatusDTO.getStatus());
        boolean result = roomsService.updateRoomStatus(id, roomStatusDTO.getStatus());
        return result ? Result.success() : Result.error("切换会议室状态失败");
    }
    
    @GetMapping("/equipment-types")
    @Operation(summary = "获取所有设备类型")
    public Result getEquipmentTypes() {
        log.info("获取所有设备类型");
        List<EquipmentType> equipmentTypes = equipmentTypeService.getAllEquipmentTypes();
        return Result.success(equipmentTypes);
    }
    
    @GetMapping("/categories")
    @Operation(summary = "获取所有会议室分类")
    public Result getAllCategories() {
        log.info("获取所有会议室分类");
        List<Category> categories = categoryService.list();
        return Result.success(categories);
    }
} 